package com.skt.common_hr.biztrip2.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.dataloader.*;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class OrgProxy extends CommonProxy
	{
		public static const NAME:String = "OrgProxy";
		
		public var orgList:XMLList;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function OrgProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, auth:String, setFunc:Function):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = StringUtil.trimFront(pernr, "0");
			param["I_AUTH"] = auth;
			loadData("Z_HR_0015", param, result, fault, setFunc); 
		}

		/**
		 * 출장현황과 상태 코드값 조회
		 */
		public function loadCodes(setFunc:Function):void
		{
			var param:Object = new Object();
			loadData("Z_HR_T022", param, loadCodesResult, fault, setFunc); 
		}
		public function loadCodesResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				var allLabel:Object = {};
				allLabel.CODE = "";
				allLabel.TEXT = "전체";
				
				var acT_TRSTATUS:ArrayCollection = event.result.T_TRSTATUS;
				var acT_TRTYPE:ArrayCollection = event.result.T_TRTYPE;
				
				acT_TRSTATUS.addItemAt(allLabel, 0);
				acT_TRTYPE.addItemAt(allLabel, 0);
				
				var obj:Object = {};
				obj.TRSTATUS = acT_TRSTATUS;
				obj.TRTYPE = acT_TRTYPE;
				event.result.func(obj);
			}
			else {
				trace( "failed to get post list");				
			}
			CursorManager.removeBusyCursor();
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				orgList = parseOrgList(event.result.RTAB);
				event.result.func(orgList);
			}
			else {
				trace( "failed to get post list");				
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);			
			CursorManager.removeBusyCursor();
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseOrgList(retObj:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
//shgoo : 인원수  HEADS 항목 추가  	2009.05.28
//			var root:XML = <node label={retObj[0].STEXT} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
			var root:XML;
			if(retObj.length > 0) {
				root = <node label={retObj[0].STEXT} heads={retObj[0].HEADS} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
	////////////////////
	
				prevArr[root.@level] = root;
				
				for(var i:int=1; i<retObj.length; i++)
				{
					var rec:Object = retObj[i];
	//shgoo : 인원수  HEADS 항목 추가  	2009.05.28
	//				var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
					var node:XML = <node label={rec.STEXT} heads={rec.HEADS} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
	///////////////////////
					if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
						XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
					}
					prevArr[node.@level] = node;
				}
			}
			else {
				root = <node label="No Data" objid="0000" level="0" parent="0000"/>
			}
				
			//trace(root.toXMLString());
			return new XMLList(root);
		}
		
	}
}